package com.lz.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

/**
 * Created by ZhouBing on 2017/8/18.
 */
public class Java2XML {

    /**
     * @param args
     * @throws JAXBException
     */
    public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(People.class);

        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING,"gb2312");//编码格式
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化生成的xml串
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息（<?xml version="1.0" encoding="gb2312" standalone="yes"?>）

        People people = new People();

        //保存到文件
        /*File file = new File("D:/people.xml");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        marshaller.marshal(people, file);*/

        //解析成xml字符串
        StringWriter result = new StringWriter();
        marshaller.marshal(people, result);
        System.out.println(result.toString().trim());



    }

}